package com.zlll.winner.controller.bbs;


import com.alibaba.fastjson.JSONObject;
import com.zlll.winner.LoginUser;
import com.zlll.winner.business.model.bbs.BbsPost;
import com.zlll.winner.business.model.bbs.BbsUserComment;
import com.zlll.winner.business.service.bbs.IBbsPostService;
import com.zlll.winner.business.service.bbs.IBbsUserCommentService;
import com.zlll.winner.common.BaseController;
import com.zlll.winner.enums.CommonEnum;
import com.zlll.winner.enums.bbs.CommentEnum;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 论坛专题管理
 */
@LoginUser
@RestController
@RequestMapping(value = "/bbs/comment")
public class BbsUserCommentController extends BaseController {

    @Reference
    private IBbsUserCommentService bbsUserCommentService;

    @Reference
    private IBbsPostService postService;

    /**
     * 分页列表
     * @param params
     * @return
     */
    @PostMapping(value = "page")
    public Object page(@RequestBody JSONObject params){
        Integer pageNo = params.getInteger("pageNo");
        Integer pageSize = params.getInteger("pageSize");
        Assert.notNull(pageNo,"pageNo不能为空");
        Assert.notNull(pageSize,"pageSize不能为空");
//        return bbsUserCommentService.page(params);
        return bbsUserCommentService.fetchCommentPage(params);
    }

    /**
     * 保存与更新
     * @param params
     * @return
     */
    @PostMapping(value = "save")
    public Object save(@RequestBody JSONObject params){
        BbsUserComment bbsUserComment = params.toJavaObject(BbsUserComment.class);
        Long postId = params.getLong("postId");
        bbsUserComment.setBizId(postId);
        bbsUserComment.setBizType(CommentEnum.BizTypeEnum.POST.val());
        if(bbsUserComment.getId()!= null){
            bbsUserComment.setUpdateTime(System.currentTimeMillis());
        }else{
            bbsUserComment.setCreateTime(System.currentTimeMillis());
        }
        bbsUserComment.setStatus(CommentEnum.StatusEnum.PUT_AWAY.val());
        boolean b = bbsUserCommentService.saveOrUpdate(bbsUserComment);
        if(b) postService.userPostActionCount(postId,3);
        return b;
    }

    /**
     * 下架功能
     * @param params
     * @return
     */
    @PostMapping(value = "update")
    public Object update(@RequestBody JSONObject params){
        Long id = params.getLong("id");
        Assert.notNull(id,"记录Id不能为空");
        BbsUserComment updateObj = new BbsUserComment();
        updateObj.setId(id);
        updateObj.setUpdateTime(System.currentTimeMillis());
        updateObj.setStatus(CommentEnum.StatusEnum.PUT_BACK.val());
        return bbsUserCommentService.updateById(updateObj);
    }

    /**
     * 删除
     * @param params
     * @return
     */
    @PostMapping(value = "delete")
    public Object delete(@RequestBody JSONObject params){
        Long id = params.getLong("id");
        Assert.notNull(id,"记录Id不能为空");
        BbsUserComment updateObj = new BbsUserComment();
        updateObj.setId(id);
        updateObj.setUpdateTime(System.currentTimeMillis());
        updateObj.setIsDel(CommonEnum.DelEnum.DEL.val());
        return bbsUserCommentService.updateById(updateObj);
    }

    /**
     * 详情
     * @param params
     * @return
     */
    @PostMapping(value = "details")
    public Object details(@RequestBody JSONObject params){
        Long id = params.getLong("id");
        Assert.notNull(id,"记录Id不能为空");
        return bbsUserCommentService.getById(id);
    }

}
